var _treeDatagrid,dialogAddRow,formAddRow,id,pid,name,code,orderIndex;

function addRow(o) {
	if (o) {
		_treeDatagrid.treegrid('select', o);
	}
	addRowHandler();
}

function addRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	id.val(0);
	pid.val(0);
	if (node && node.length > 0) {
		pid.val(node[0].id);
	}
	$('#name').val('');
	$('#code').val('');
	$('#orderIndex').slider('setValue', 0);
	dialogAddRow.dialog({title:'添加字典',iconCls:'icon-add',buttons:[{text:'添加',handler:dialogEnterHandler}]});
	dialogAddRow.dialog('open');
}

/**添加或更新对话框*/
function dialogEnterHandler() {
	if (formAddRow.form('validate')) {
		var url = sy.bp() + '/admin/dictionary/';
		if (id.val() == 0) {
			url += 'add';
		} else {
			url += 'update';
		}
		$.post(url, $('#formAddRow').serialize(), function(data){
			if(data && data.result ==1) {
				dialogAddRow.dialog('close');
				$.messager.show({title:'系统提示', msg:data.msg, timeout:2000, showType:'slide'});
				_treeDatagrid.treegrid('reload');
			} else {
				$.messager.alert('错误提示', data.msg);
			}
		},"json");
	}
}

function deleteRow(o) {
	if (o) {
		_treeDatagrid.treegrid('select', o);
	}
	deleteRowHandler();
}

function deleteRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？注意：此操作将删除所有字典！！！', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < node.length; i++) {
					ids.push(node[i].id);
				}
				$.post(sy.bp()+"/admin/dictionary/deletes", {ids:ids.join(",")}, function(data) {
					if (data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						_treeDatagrid.treegrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
}

function updateRow(o) {
	if (o) {
		_treeDatagrid.treegrid('select', o);
	}
	updateRowHandler();
}

function updateRowHandler() {
	var node = _treeDatagrid.treegrid('getSelections');
	if (node && node.length > 0) {
		if (node.length > 1) {
			$.messager.alert('提示', '您选择了多个菜单项,只能选择一个菜单项！', 'warning');
			_treeDatagrid.treegrid('unselectAll');
		} else {
			id.val(node[0].id);
			pid.val(node[0].pid);
			$('#name').val(node[0].name);
			$('#code').val(node[0].code);
			$('#orderIndex').slider('setValue', node[0].orderIndex);
			dialogAddRow.dialog({title:'更新字典',iconCls:'icon-edit',buttons:[{text:'更新',handler:dialogEnterHandler}]});
			dialogAddRow.dialog('open');
		}
	} else {
		$.messager.alert('提示', '您没有选择菜单项,请选择！', 'warning');
	}
}

function reloadRow() {
	_treeDatagrid.treegrid('reload');
}

function unselectRow() {
	_treeDatagrid.treegrid('unselectAll');
}

$(function() {
	_treeDatagrid = $('#_treeDatagrid');
	id = $('#id');
	pid = $('#pid');
	dialogAddRow = $('#dialogAddRow');
	formAddRow = $('#formAddRow');

	dialogAddRow.show().dialog({resizable:false,closed:true,closable:true,modal:true});
	formAddRow.form({});
	name = $('#name').validatebox({required:true,validType:['maxLength[100]']});
	code = $('#code').validatebox({validType:['maxLength[100]']});
	orderIndex = $('#orderIndex').slider({value:0,min:0,max:9,showTip:true});
	_treeDatagrid.treegrid({   
	    url: sy.bp()+'/admin/dictionary/listTreegrid',
	    idField:'id',
	    treeField:'name',
	    fit : true,
		autoRowHeight : true,
		striped : true,
		rownumbers : true,
		singleSelect : false,
		fitColumns : true,
		nowrap : false,
		border : false,
		toolbar: [
			{text:'增加',iconCls: 'icon-add',handler: addRowHandler},'-',
			{text:'删除',iconCls: 'icon-remove',handler: deleteRowHandler},'-',
			{text:'修改',iconCls: 'icon-edit',handler: updateRowHandler},'-',
			{text:'取消选中',iconCls: 'icon-redo',handler: unselectRow},'-',
			{text:'刷新',iconCls: 'icon-reload',handler: reloadRow},'-'
		 ],
		frozenColumns: [[]],
	    columns:[[
	  	    {field:'name',title:'名称',width:500,align:'left'},
	        {field:'code',title:'编码',width:100,align:'left'},
	        {field:'orderIndex',title:'排序',width:100,align:'center'},
	    ]],
	    onBeforeExpand : function(){
	    },
	    onLoadSuccess : function(data) {
			$.messager.progress('close');
		},
		onDblClickRow : function(row){
			if (row.id > 0) {
				updateRow(row.id);
			} else {
				_treeDatagrid.treegrid('toggle', row.id);
			}
		},
		onContextMenu : function(e, row){
			_treeDatagrid.treegrid('unselectAll');
			_treeDatagrid.treegrid('select', row.id);
			e.preventDefault();
			$('#treeridContextMenu').menu('show', {left:e.pageX,top:e.pageY});
		}
	});
});
